package com.xijin.milkytea.ui.meal

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import cn.net.hylink.launcher5.base.BaseFragment
import com.jeremyliao.liveeventbus.LiveEventBus
import com.xijin.milkytea.MyApplication
import com.xijin.milkytea.R
import com.xijin.milkytea.databinding.FragmentMealBinding
import com.xijin.milkytea.livedata.Type.Companion.TYPE_NOTICE_UPDATE_SPECIFICATION_MEAL_PREPARE
import com.xijin.milkytea.logic.Repository
import com.xijin.milkytea.logic.dao.AppDataBase
import com.xijin.milkytea.logic.entity.Cup
import com.xijin.milkytea.logic.entity.Material
import com.xijin.milkytea.logic.entity.MaterialType
import com.xijin.milkytea.logic.entity.Order
import com.xijin.milkytea.logic.entity.relation.OrderWithRecipe
import com.xijin.milkytea.ui.adapter.MealPrepareAdapter
import kotlin.concurrent.thread

//备餐
class MealFragment : BaseFragment<MealViewModel, FragmentMealBinding>() {

    private val TAG: String = "MealFragment"
    lateinit var adapter: MealPrepareAdapter

    override fun layoutId() = R.layout.fragment_meal

    override fun initView(savedInstanceState: Bundle?) {
        val materialDao = AppDataBase.getDataBase(MyApplication.context).materialDao()
        val materialTypeDao = AppDataBase.getDataBase(MyApplication.context).materialTypeDao()
        val materialType1 = MaterialType("牛奶")
        val materialType2 = MaterialType("茶汤")

        val material1 = Material("纯牛奶", materialType1.id)
        val material2 = Material("脱脂牛奶", materialType1.id)
        val material3 = Material("乌龙茶", materialType2.id)
        val material4 = Material("绿茶", materialType2.id)

        val cup1 = Cup("大杯")
        val cup2 = Cup("中杯")
        val cup3 = Cup("小杯")
        val cup4 = Cup("超大杯")
        val cup5 = Cup("超小杯")

        mDatabind.apply {
            fmBtn0.setOnClickListener {
                startActivity(Intent(mActivity, ManualMealPrepareActivity::class.java))
                //手动备餐
                /*thread {
                    materialType1.id = materialTypeDao.insertMaterialType(materialType1)
                    materialType2.id = materialTypeDao.insertMaterialType(materialType2)
                    Repository.insertCup(cup1)
                    Repository.insertCup(cup2)
                    Repository.insertCup(cup3)
                    Repository.insertCup(cup4)
                    Repository.insertCup(cup5)
                }*/
            }
            fmBtn1.setOnClickListener {
                //屏幕解锁
                /*thread {
                    materialType1.typeName = "牛牛奶"
                    materialTypeDao.updateMaterialType(materialType1)
                }*/
                mDatabind.progressBar.setProgressException(true)
                mDatabind.progressBar.setProgress(0.25f)
                mDatabind.progressBar.isCanScroll = false
            }
            fmBtn2.setOnClickListener {
                //取消订单
                /*thread {
                    materialTypeDao.deleteMaterialTypeByTypeName("牛奶")
                }*/
                val checkedOrder = mutableListOf<OrderWithRecipe>()
                for (item in adapter.data) {
                    if (item.isChecked) {
                        checkedOrder.add(item)
                    }
                }
                Log.e(TAG, "initView: checkedOrder ${checkedOrder.size}")
                mViewModel.updateOrderFailure(checkedOrder)
            }
            fmBtn3.setOnClickListener {
                mDatabind.progressBar.setProgressException(false)
                mDatabind.progressBar.setProgress(0.5f)

                //手动刷新
                /*thread {
                    for (materialType in materialTypeDao.loadAllMaterialType()) {
                        Log.e(TAG, "initView: id: ${materialType.id} name:${materialType.typeName}")
                    }
                    Log.e(TAG, "initView:``````````````````` " )
                }*/
                mViewModel.queryOrders(status = Order.STATUS_ORDER_PREPARE)
            }
            fmBtn4.setOnClickListener {
                //搜索
                mViewModel.queryOrders(status = Order.STATUS_ORDER_PREPARE, code = fmEt0.text.toString().trim())
            }
//            var list = listOf<String>("", "", "", "", "", "", "", "", "", "", "")
            adapter = MealPrepareAdapter()
            adapter.setOnItemClickListener { adapter, view, positon ->

            }
            adapter.addChildClickViewIds(R.id.tv04, R.id.tv6)
            adapter.setOnItemChildClickListener { adapter, view, positon ->
                when (view.id) {
                    R.id.tv04 -> {
                        var intent = Intent(mActivity, UpdateSpecificationActivity::class.java)
                        var datas = mViewModel.ordersData.value ?: mutableListOf()
                        /* var bundle =Bundle()
                         bundle.putParcelable("key",datas[positon])
                         intent.putExtra("bundle",bundle)*/
                        LiveEventBus.get<OrderWithRecipe>(TYPE_NOTICE_UPDATE_SPECIFICATION_MEAL_PREPARE).post(datas[positon])
                        startActivity(intent)
                        Log.e(TAG, "childClick 修改规格 tv04 position:$positon ")
                    }
                    R.id.tv6 -> {
                        Log.e(TAG, "childClick 出茶 tv6 position:$positon ")
                    }
                }
            }
            fmRv.layoutManager = LinearLayoutManager(requireContext())
            fmRv.adapter = adapter
        }
    }

    override fun onResume() {
        super.onResume()
        //查询待制作订单
        mViewModel.queryOrders(status = Order.STATUS_ORDER_PREPARE)
    }

    override fun createObserver() {
        mViewModel.ordersData.observe(this) {
            adapter.data = it.toMutableList()
            adapter.notifyDataSetChanged()
        }

        mViewModel.updateResultData.observe(this) {
            mViewModel.queryOrders(status = Order.STATUS_ORDER_PREPARE)
        }
    }
}